﻿using DAMS.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace DAMS.Data
{
    public class DbContextSeed
    {
        private UserManager<AppUser> _userManager;
        public async Task SeedAsync(DbCon context, IServiceProvider service)
        {
            if (!context.Users.Any())
            {
                _userManager = service.GetRequiredService<UserManager<AppUser>>();
                
                //创建初始用户
                var defultUser = new AppUser()
                {
                    UserName = "Administrator",
                    Email = "453151742@qq.com",
                    NormalizedUserName = "admin"
                };
                var result = await _userManager.CreateAsync(defultUser, "Password$123");
                if (!result.Succeeded)
                {
                    throw new Exception("初始用户创建失败");
                }
            }
        }
    }

   
}
